package cruds.services;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;

import cruds.CrudCreateParent;
import dynamicmenus.ServicesMenu;
import exceptions.EmptyFieldException;
import items.Service;
import home.Home;
import panels.BodyPanel;
import services.ServiceService;
import staticmenus.StaticMenu;
import utils.StaticMethods;

@SuppressWarnings("serial")
public class CrudCreateService extends CrudCreateParent<Service>{
	
	public CrudCreateService(Home home) {
		super(home);
	}


	@Override
	protected void redirectToPageIfCancel(AjaxRequestTarget target) {
		ServiceList page = new ServiceList(this.getHome());
		page.changePanel(target);
	}

	@Override
	protected void updateDynamicMenu(AjaxRequestTarget target) {
	     ServicesMenu menu = new ServicesMenu(this.getHome());
	     menu.changePanel(target);
	}

	@Override
	protected void doSaveAndRedirect(AjaxRequestTarget target) {
		try{
			StaticMethods.serviceHasEmptyFields(this.getItem());
			this.getService().save(this.getItem());
			ServiceList page = new ServiceList(this.getHome());
			page.changePanel(target);
		}catch(EmptyFieldException e){
			this.setErrorMessage(target, e.getMessage());
		}
		
	}

	@Override
	protected void createFormFields(Form<Service> form) {
		form.add(new TextField<String>("name"));
		form.add(new TextField<String>("salePrice"));	
	}

	@Override
	protected void initializeEmptyItem() {
		this.setItem(new Service());
	}


	@Override
	protected void initializeService() {
		this.setService(this.getHome().getGeneralService().getServiceSer());
		
	}

}
